#include "TMClientSettingsDlg.h"

#include <QSettings>
#include <QtDebug>

CTMClientSettingsDlg::CTMClientSettingsDlg( QWidget * parent, Qt::WFlags f) 
	: QDialog(parent, f)
{
	setupUi(this);
	
	// make the reset button clear down all fields:
	QPushButton *btnReset = buttonBox->button(QDialogButtonBox::Reset);
	QPushButton *btnApply = buttonBox->button(QDialogButtonBox::Apply);
	QPushButton *btnOK = buttonBox->button(QDialogButtonBox::Ok);

	// Okay - WTF... why do I have to explicitly cast these? That's nuts!
	connect((const QObject*)btnReset, SIGNAL(clicked()), SLOT(loadValues()));
	connect((const QObject*)btnApply, SIGNAL(clicked()), SLOT(saveValues()));
	connect((const QObject*)btnOK, SIGNAL(clicked()), SLOT(saveValues()));
	
	loadValues();
}
//

void CTMClientSettingsDlg::loadValues()
{
	qDebug() << "Loading values";
	QSettings settings;
	
	ServerIPAddr->setText(settings.value("config/serverAddress", "").toString());
	ServerPort->setValue(settings.value("config/serverPort", 4567).toInt());
	Username->setText(settings.value("config/username", "").toString());
	Password->setText(settings.value("config/password", "").toString());
}

void CTMClientSettingsDlg::saveValues()
{
	qDebug() << "Saving values";
	QSettings settings;
	
	settings.setValue("config/serverAddress", ServerIPAddr->text());
	settings.setValue("config/serverPort", ServerPort->value());
	settings.setValue("config/username", Username->text());
	settings.setValue("config/password", Password->text());	// TODO - store the hash, not the password!
}

